import { userList } from '../utils/socketList'
import { Request, Response } from 'express';
import { UserModel } from '../models/user';
import { UserInfoModel } from '../models/userInfoModel';
export class TalkController {
  private userModel: UserModel;
  private userInfoModel: UserInfoModel;
  constructor() {
    this.userModel = new UserModel();
    this.userInfoModel = new UserInfoModel();
  }
  sendMessageByUserId = async (req: Request, res: Response) => {
    const user = req.user!;
    const loginInfo = await this.userModel.findByUserId(user.userId as string);
    const sendInfo = await this.userInfoModel.findByUserId(user.userId as string);
    const { username } = loginInfo!;
    const { headImg } = sendInfo!;
    const { targetUserId, message, type } = req.body;
    const io = userList.get(targetUserId)
    if (io) {
      io.socket.emit('callme', {
        sendUserId: user.userId,
        username,
        headImg,
        message,
        type
      })
      res.json({ code: 200, message: '发送成功' });
      return;
    }
    res.status(200).json({ code: 204, message: '用户离线' });
  }
}
